package kayao.client.scene.objects;

import javax.microedition.khronos.opengles.GL10;

import kayao.common.Client;
import kayao.client.R;
import kayao.client.data.SharedMemory;
import kayao.client.scene.PhysicsController;

import android.opengl.Matrix;

import com.chopsticks3d.math.MatrixMath;
import com.chopsticks3d.scene.Material;
import com.chopsticks3d.scene.Mesh;
import com.chopsticks3d.scene.MeshNode;
import com.chopsticks3d.scene.Texture;

/**
* Represents a car in the networked virtual environment.
* It has the name of the owner above it if the owner is not
* the local client, which is the case if the owner is null.
*/
public class CarNode extends MovingEntity {
	private static Mesh sCarMesh = null;
	private static final Texture tex = new Texture(R.drawable.tank1);
	
	private MeshNode mCarNode;
	private MeshNode mNameNode;
	private String mName;
	
	// Temporary for moving around
	public float deltaRotation;
	
	private Boolean mPhysicsInitialized = false;
	
	/**
	 * Constructor which set the owner.
	 * @param owner the owner of the entity.
	 */
	public CarNode() {
		this(null);
	}
	
	/**
	 * Constructor which set the owner.
	 * @param owner the owner of the entity.
	 */
	public CarNode(Client owner) {
		super(owner);
		
		if (sCarMesh == null) {
			sCarMesh = new Mesh();
			sCarMesh.importModel(R.raw.tank, SharedMemory.getInstance().getContext());
		}
		
		if (owner != null) {
			mName = owner.getName();
			
			boolean teammate = owner.getTeam() == SharedMemory.getInstance().getLocalTeamId();
			mNameNode = new CarNameNode(mName, teammate);
			mNameNode.setTranslationY(1.5f);
			
			addChild(mNameNode);
		}
		
		// Create the scene of the car
		Material carMaterial = new Material();
		carMaterial.setAmbient(1, 1, 1, 1);
		mCarNode = new MeshNode(sCarMesh, tex, carMaterial);
//		mCarNode.setTranslationY(0.5f);
		
		addChild(mCarNode);
	}
	
	@Override
	public void setOwner(Client owner) {
		super.setOwner(owner);
		if (owner != null) {
			mName = owner.getName();
		}
	}
	
	@Override
	public void onDraw(GL10 gl) {
		if (!mPhysicsInitialized) {
			mBodyId = PhysicsController.createDynamicBox(mX, mZ, getScaleX(), getScaleZ() + 0.5f, 0.1f, 0.1f, 0.1f);			
			mPhysicsInitialized = true;
		}
		
		super.onDraw(gl);
	}
	
	@Override
	public void update() {
		if (this.getBodyId() != -1) {
			PhysicsController.updateBody(this, getBodyId());
		}
		
		super.update();
	}
	
	public void updateAvatarTransformation() {
		if (mParent != null) {
			System.arraycopy(mParent.getTransformation(), 0, mModelView, 0, 16);
		} else {
			Matrix.setIdentityM(mModelView, 0);
		}
		
		Matrix.translateM(mModelView, 0, mX, mY, mZ);
		MatrixMath.rotateM(mModelView, mRotationX, MatrixMath.AXIS_X);
		MatrixMath.rotateM(mModelView, mRotationY, MatrixMath.AXIS_Y);
		MatrixMath.rotateM(mModelView, mRotationZ, MatrixMath.AXIS_Z);
		Matrix.scaleM(mModelView, 0, mScaleX, mScaleY, mScaleZ);
	}
}
